home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmptrap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  6.6 KB  |  243 lines

  1. /*
  2.  * snmptrap.c - send snmp traps to a network entity.
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <netdb.h>
  29. #include <stdio.h>
  30. #include <sys/time.h>
  31. #include <sys/socket.h>
  32. #include <net/if.h>
  33. #include <sys/ioctl.h>
  34. #include <sys/file.h>
  35. #include <nlist.h>
  36.  
  37. #include "snmp.h"
  38. #include "snmp_impl.h"
  39. #include "asn1.h"
  40. #include "snmp_api.h"
  41. #include "snmp_client.h"
  42.  
  43. extern int  errno;
  44. int    snmp_dump_packet = 0;
  45.  
  46. #define NUM_NETWORKS    16   /* max number of interfaces to check */
  47.  
  48. oid objid_enterprise[] = {1, 3, 6, 1, 4, 1, 3, 1, 1};
  49. oid objid_sysdescr[] = {1, 3, 6, 1, 2, 1, 1, 1, 0};
  50.  
  51. struct nlist nl[] = {
  52.     { "_boottime" },
  53.     { "" }
  54. };
  55.  
  56.  
  57. int snmp_input(){
  58. }
  59.  
  60. #ifndef IFF_LOOPBACK
  61. #define IFF_LOOPBACK 0
  62. #endif
  63. #define LOOPBACK    0x7f000001
  64. u_long
  65. get_myaddr(){
  66.     int sd;
  67.     struct ifconf ifc;
  68.     struct ifreq conf[NUM_NETWORKS], *ifrp, ifreq;
  69.     struct sockaddr_in *in_addr;
  70.     int count;
  71.     int interfaces;        /* number of interfaces returned by ioctl */
  72.  
  73.     if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  74.     return 0;
  75.     ifc.ifc_len = sizeof(conf);
  76.     ifc.ifc_buf = (caddr_t)conf;
  77.     if (ioctl(sd, SIOCGIFCONF, (char *)&ifc) < 0){
  78.     close(sd);
  79.     return 0;
  80.     }
  81.     ifrp = ifc.ifc_req;
  82.     interfaces = ifc.ifc_len / sizeof(struct ifreq);
  83.     for(count = 0; count < interfaces; count++, ifrp++){
  84.     ifreq = *ifrp;
  85.     if (ioctl(sd, SIOCGIFFLAGS, (char *)&ifreq) < 0)
  86.         continue;
  87.     in_addr = (struct sockaddr_in *)&ifrp->ifr_addr;
  88.     if ((ifreq.ifr_flags & IFF_UP)
  89.         && (ifreq.ifr_flags & IFF_RUNNING)
  90.         && !(ifreq.ifr_flags & IFF_LOOPBACK)
  91.         && in_addr->sin_addr.s_addr != LOOPBACK){
  92.         close(sd);
  93.         return in_addr->sin_addr.s_addr;
  94.         }
  95.     }
  96.     close(sd);
  97.     return 0;
  98. }
  99.  
  100. /*
  101.  * Returns uptime in centiseconds(!).
  102.  */
  103. long uptime(){
  104.     struct timeval boottime, now, diff;
  105.     int kmem;
  106.  
  107.     if ((kmem = open("/dev/kmem", 0)) < 0)
  108.     return 0;
  109.     nlist("/vmunix", nl);
  110.     if (nl[0].n_type == 0){
  111.     close(kmem);
  112.     return 0;
  113.     }
  114.     
  115.     lseek(kmem, (long)nl[0].n_value, L_SET);
  116.     read(kmem, &boottime, sizeof(boottime));
  117.     close(kmem);
  118.  
  119.     gettimeofday(&now, 0);
  120.     now.tv_sec--;
  121.     now.tv_usec += 1000000L;
  122.     diff.tv_sec = now.tv_sec - boottime.tv_sec;
  123.     diff.tv_usec = now.tv_usec - boottime.tv_usec;
  124.     if (diff.tv_usec > 1000000L){
  125.     diff.tv_usec -= 1000000L;
  126.     diff.tv_sec++;
  127.     }
  128.     return ((diff.tv_sec * 100) + (diff.tv_usec / 10000));
  129. }
  130.  
  131. u_long parse_address(address)
  132.     char *address;
  133. {
  134.     u_long addr;
  135.     struct sockaddr_in saddr;
  136.     struct hostent *hp;
  137.  
  138.     if ((addr = inet_addr(address)) != -1)
  139.     return addr;
  140.     hp = gethostbyname(address);
  141.     if (hp == NULL){
  142.     fprintf(stderr, "unknown host: %s\n", address);
  143.     return 0;
  144.     } else {
  145.     bcopy((char *)hp->h_addr, (char *)&saddr.sin_addr, hp->h_length);
  146.     return saddr.sin_addr.s_addr;
  147.     }
  148.  
  149. }
  150. main(argc, argv)
  151.     int        argc;
  152.     char    *argv[];
  153. {
  154.     struct snmp_session session, *ss;
  155.     struct snmp_pdu *pdu;
  156.     struct variable_list *vars;
  157.     int    arg;
  158.     char *gateway = NULL;
  159.     char *community = NULL;
  160.     char *trap = NULL, *specific = NULL, *description = NULL, *agent = NULL;
  161.  
  162.  
  163.     /*
  164.      * usage: snmptrap gateway-name community-name trap-type specific-type device-description [ -a agent-addr ]
  165.      */
  166.     for(arg = 1; arg < argc; arg++){
  167.     if (argv[arg][0] == '-'){
  168.         switch(argv[arg][1]){
  169.         case 'a':
  170.             agent = argv[++arg];
  171.             break;
  172.         case 'd':
  173.             snmp_dump_packet++;
  174.             break;
  175.         default:
  176.             printf("invalid option: -%c\n", argv[arg][1]);
  177.             break;
  178.         }
  179.         continue;
  180.     }
  181.     if (gateway == NULL){
  182.         gateway = argv[arg];
  183.     } else if (community == NULL){
  184.         community = argv[arg]; 
  185.     } else if (trap == NULL){
  186.         trap = argv[arg];
  187.     } else if (specific == NULL){
  188.         specific = argv[arg];
  189.     } else {
  190.         description = argv[arg];
  191.     }
  192.     }
  193.  
  194.     if (!(gateway && community && trap && specific && description)){
  195.     printf("usage: snmptrap host community trap-type specific-type device-description [ -a agent-addr ]\n");
  196.     exit(1);
  197.     }
  198.  
  199.     bzero((char *)&session, sizeof(struct snmp_session));
  200.     session.peername = gateway;
  201.     session.community = (u_char *)community;
  202.     session.community_len = strlen((char *)community);
  203.     session.retries = SNMP_DEFAULT_RETRIES;
  204.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  205.     session.authenticator = NULL;
  206.     session.callback = snmp_input;
  207.     session.callback_magic = NULL;
  208.     session.remote_port = SNMP_TRAP_PORT;
  209.     ss = snmp_open(&session);
  210.     if (ss == NULL){
  211.     printf("Couldn't open snmp\n");
  212.     exit(-1);
  213.     }
  214.  
  215.     pdu = snmp_pdu_create(TRP_REQ_MSG);
  216.     pdu->enterprise = (oid *)malloc(sizeof(objid_enterprise));
  217.     bcopy((char *)objid_enterprise, (char *)pdu->enterprise, sizeof(objid_enterprise));
  218.     pdu->enterprise_length = sizeof(objid_enterprise) / sizeof(oid);
  219.     if (agent != NULL)
  220.     pdu->agent_addr.sin_addr.s_addr = parse_address(agent);
  221.     else
  222.     pdu->agent_addr.sin_addr.s_addr = get_myaddr();
  223.     pdu->trap_type = atoi(trap);
  224.     pdu->specific_type = atoi(specific);
  225.     pdu->time = uptime();
  226.  
  227.     pdu->variables = vars = (struct variable_list *)malloc(sizeof(struct variable_list));
  228.     vars->next_variable = NULL;
  229.     vars->name = (oid *)malloc(sizeof(objid_sysdescr));
  230.     bcopy((char *)objid_sysdescr, (char *)vars->name, sizeof(objid_sysdescr));
  231.     vars->name_length = sizeof(objid_sysdescr) / sizeof(oid);
  232.     vars->type = ASN_OCTET_STR;
  233.     vars->val.string = (u_char *)malloc(strlen(description) + 1);
  234.     strcpy((char *)vars->val.string, description);
  235.     vars->val_len = strlen(description);
  236.  
  237.     if (snmp_send(ss, pdu)== 0){
  238.     printf("error\n");
  239.     }
  240.     snmp_close(ss);
  241. }
  242.  
  243.